package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.Constant;
import com.apache.bid.common.WbdStringUtil;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidSessionManagerImpl implements BidSessionManager {

	protected IDao bidSessionDao;
	protected final String entityName = "com.apache.bid.entity.BidSession";

	public void setBidSessionDao(IDao bidSessionDao) {
		this.bidSessionDao = bidSessionDao;
	}

	public String saveInfo(ParamsVo<BidSession> vo) throws BusinessException {
		BidSession bidSession = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidSession_"+infoId;
		if (Validator.isNull(bidSession.getSessionId())) {
			bidSession.setSessionId(infoId);
		}
		if (Constant.getInstance().ifWriteDatabase()) {
			if (Validator.isNull(bidSession.getIfBidDyn())) {///是否动态报价（T/F）
				bidSession.setIfBidDyn("T");
			}
			if (Validator.isNull(bidSession.getStatus())) {//状态：0=待审核、1=审核中，2=已发布，3=暂停中
				bidSession.setStatus("0");
			}
			if (Validator.isNull(bidSession.getBailCtrl())) {//是否交纳保证金(0=不交，1=按固定，2=按数量，3=按比例）
				bidSession.setBailCtrl("0");
			}
			if (Validator.isNull(bidSession.getAllowApply())) {//是否允许对外报价申请（缺省开放的）极少使用(T/F)
				bidSession.setAllowApply("T");
			}
			if (Validator.isNull(bidSession.getExceptionStatus())) {//是否异常
				bidSession.setExceptionStatus("0");
			}
			if (Validator.isNull(bidSession.getSerialCtrl())) {//(T/F) F   控制方式 是否串行
				bidSession.setSerialCtrl("F");
			}
			bidSession.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			bidSession.setDirection(Validator.getDefaultStr(bidSession.getDirection(), "T"));//方向：默认正（T)、反（F）
			MethodParam param = new MethodParam("BidSession", cacheKey, "", entityName);
			param.setVaule(bidSession);
			if (bidSessionDao.insert(param)) {
				return bidSession.getSessionId();
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			return bidSession.getSessionId();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidSession> vo) throws BusinessException {
		BidSession bidSession = vo.getObj();
		if (Validator.isNotNull(bidSession.getSessionId())) {
			String cacheKey = "";
			//String cacheKey="bidSession_"+bidSession.getSessionid();
			if (Constant.getInstance().ifWriteDatabase()) {
				MethodParam param = new MethodParam("BidSession", cacheKey, "", entityName);
				bidSession.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
				param.setVaule(bidSession);
				boolean mark = bidSessionDao.edit(param);
				if (mark) {
					param.setKey("ById");
					param.setInfoId(bidSession.getSessionId());
					bidSession = (BidSession) bidSessionDao.selectById(param);
					if (!Constant.getInstance().getSessionConfig("statusTemp").equals(bidSession.getStatus())) {
						BidCacheFactory.getInstance().getCacheManager("session").updateCacheObject(bidSession);
					}
				}
				return mark;
			} else {
				///////////////需要处理缓存结果//////////////////
				vo.setParams("methodCode", "editSession");
				return true;
			}
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidSession> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidSession_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		if (Constant.getInstance().ifWriteDatabase()) {
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			if ("false".equals(mark)) {//逻辑删除
				param.setInfoId(infoId);
				BidSession info = (BidSession) bidSessionDao.selectById(param);
				if (Validator.isEmpty(info)) {
					return false;
				}
				param.setVaule(info);//此处需要先将状态值赋值为删除状态
				param.setKey("BidSession");
				return bidSessionDao.edit(param);
			} else {
				param.setParams("sessionId", infoId);
				param.setDelete(true);
				return bidSessionDao.delete(param);
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			if ("false".equals(mark)) {//逻辑删除
			} else {
			}
			vo.setParams("methodCode", "deleteSession");
			return true;
		}
	}

	public Object getInfoById(ParamsVo<BidSession> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidSession_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidSessionDao.selectById(param);
	}

	public Object execute(ParamsVo<BidSession> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidSession> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		//param.setOrderby(" create_time desc");
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidSessionDao.pageSelect(param);
		return page;
	}

	public List<BidSession> getList(ParamsVo<BidSession> vo) {
		MethodParam param = setMethodParams(vo, 2);
		//param.setOrderby(" create_time desc");
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidSessionDao.select(param);
	}

	public long countInfo(ParamsVo<BidSession> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidSessionDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidSession> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidSessionDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidSession bidSession = vo.getObj();
		param.setResultCache(false);
		if (!Constant.getInstance().ifWriteDatabase()) {
			param.setResultCache(false);
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("statuses")))) {
			param.setParams("statuses", String.valueOf(vo.getParams("statuses")));
		}
		if (Validator.isEmpty(bidSession))
			return param;
		if (Validator.isNotNull(bidSession.getSessionId())) {
			param.setParams("sessionId", bidSession.getSessionId());
		}
		if (Validator.isNotNull(bidSession.getOrgEname())) {
			param.setParams("orgEname", bidSession.getOrgEname());
		}
		if (Validator.isNotNull(bidSession.getOrgCname())) {
			param.setParams("orgCname", bidSession.getOrgCname());
		}
		if (Validator.isNotNull(bidSession.getSessionName())) {
			String value = WbdStringUtil.getChUTF8(bidSession.getSessionName());
			param.setParams("sessionName", value);
		}
		if (Validator.isNotNull(bidSession.getSessionNo())) {
			param.setParams("sessionNo", bidSession.getSessionNo());
		}
		if (Validator.isNotNull(bidSession.getSessionType())) {
			param.setParams("sessionType", bidSession.getSessionType());
		}
		if (Validator.isNotNull(bidSession.getSessionMode())) {
			param.setParams("sessionMode", bidSession.getSessionMode());
		}
		if (Validator.isNotNull(bidSession.getIfBidDyn())) {
			param.setParams("ifBidDyn", bidSession.getIfBidDyn());
		}
		if (Validator.isNotNull(bidSession.getStatus())) {
			param.setParams("status", bidSession.getStatus());
		}
		if (Validator.isNotNull(bidSession.getBailCtrl())) {
			param.setParams("bailCtrl", bidSession.getBailCtrl());
		}
		if (Validator.isNotNull(bidSession.getBailFix())) {
			param.setParams("bailFix", bidSession.getBailFix());
		}
		if (Validator.isNotNull(bidSession.getBailRate())) {
			param.setParams("bailRate", bidSession.getBailRate());
		}
		if (Validator.isNotNull(bidSession.getBailMax())) {
			param.setParams("bailMax", bidSession.getBailMax());
		}
		if (Validator.isNotNull(bidSession.getCreateTime())) {
			param.setParams("createTime", bidSession.getCreateTime());
		}
		if (Validator.isNotNull(bidSession.getModifyTime())) {
			param.setParams("modifyTime", bidSession.getModifyTime());
		}
		if (Validator.isNotNull(bidSession.getAuditTime())) {
			param.setParams("auditTime", bidSession.getAuditTime());
		}
		if (Validator.isNotNull(bidSession.getArea())) {
			param.setParams("area", bidSession.getArea());
		}
		if (Validator.isNotNull(bidSession.getCity())) {
			param.setParams("city", bidSession.getCity());
		}
		if (Validator.isNotNull(bidSession.getOrderNumber())) {
			param.setParams("orderNumber", bidSession.getOrderNumber());
		}
		if (Validator.isNotNull(bidSession.getAllowApply())) {
			param.setParams("allowApply", bidSession.getAllowApply());
		}
		if (Validator.isNotNull(bidSession.getSessionLayout())) {
			param.setParams("sessionLayout", bidSession.getSessionLayout());
		}
		if (Validator.isNotNull(bidSession.getSessionCss())) {
			param.setParams("sessionCss", bidSession.getSessionCss());
		}
		if (Validator.isNotNull(bidSession.getExceptionStatus())) {
			param.setParams("exceptionStatus", bidSession.getExceptionStatus());
		}
		if (Validator.isNotNull(bidSession.getRightCtrl())) {
			param.setParams("rightCtrl", bidSession.getRightCtrl());
		}
		if (Validator.isNotNull(bidSession.getSpecialCode())) {
			param.setParams("specialCode", bidSession.getSpecialCode());
		}
		if (Validator.isNotNull(bidSession.getSenature())) {
			param.setParams("senature", bidSession.getSenature());
		}
		if (Validator.isNotNull(bidSession.getSpare1())) {
			param.setParams("spare1", bidSession.getSpare1());
		}
		if (Validator.isNotNull(bidSession.getSpare2())) {
			param.setParams("spare2", bidSession.getSpare2());
		}
		if (Validator.isNotNull(bidSession.getSpare3())) {
			param.setParams("spare3", bidSession.getSpare3());
		}
		if (Validator.isNotNull(bidSession.getSpare4())) {
			param.setParams("spare4", bidSession.getSpare4());
		}
		if (Validator.isNotNull(bidSession.getSpare5())) {
			param.setParams("spare5", bidSession.getSpare5());
		}
		if (Validator.isNotNull(bidSession.getPicUrl())) {
			param.setParams("picUrl", bidSession.getPicUrl());
		}

		if (Validator.isNotNull(bidSession.getOpenTime())) {
			param.setParams("openTime", bidSession.getOpenTime());
		}
		if (Validator.isNotNull(bidSession.getSerialCtrl())) {
			param.setParams("serialCtrl", bidSession.getSerialCtrl());
		}
		if (Validator.isNotNull(bidSession.getDirection())) {
			param.setParams("direction", bidSession.getDirection());
		}
		if (Validator.isNotNull(bidSession.getIfGroup())) {
			param.setParams("ifGroup", bidSession.getIfGroup());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
